<template>	
    <div class="index-lists">		
        <el-card class="!border-none" shadow="never">
			<el-alert
			    type="warning"
			    title="操作提示：查看并管理平台所有自营和入驻商家。"
			    :closable="false"
			    show-icon
			/>
			
            <el-form ref="formRef" class="mb-[-16px] mt-[16px]" :model="queryParams" :inline="true">                
                <el-form-item label="门店名称" prop="storeName">
                    <el-input class="w-[280px]" clearable v-model="queryParams.storeName" />
                </el-form-item>
				
                <el-form-item label="定价方式" prop="priceType">
                    <el-select
                        v-model="queryParams.priceType"
                        class="w-[280px]"
                        clearable
                    >										
                        <el-option label="总部定价" value="0" />
						<el-option label="门店定价" value="1" />
                    </el-select>
                </el-form-item>
				
                <el-form-item label="状态" prop="status">
                    <el-select
                        v-model="queryParams.status"
                        class="w-[280px]"
                        clearable
                    >
                        <el-option label="营业" value="1" />
						<el-option label="停业" value="0" />
                    </el-select>
                </el-form-item>
                
                <el-form-item>
                    <el-button type="primary" @click="resetPage">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none mt-4" shadow="never">
            <div>
                <el-button v-perms="['store:add']" type="primary" @click="handleAdd()">
                    <template #icon>
                        <icon name="el-icon-Plus" />
                    </template>
                    新增门店
                </el-button>
            </div>
            <el-table
                class="mt-4"
                size="large"
                v-loading="pager.loading"
                :data="pager.lists"
            >
                <el-table-column label="门店编号" prop="storeCode" min-width="100" />
                <el-table-column label="门店名称" prop="storeName" min-width="100" />
				<el-table-column label="定价方式" prop="priceType" min-width="100" >
					<template #default="{ row }">
						<span v-if="row.priceType==0">总部定价</span>
						<span v-if="row.priceType==1">门店定价</span>
					</template>
				</el-table-column>
                <el-table-column label="门店账号" prop="username" min-width="120" />
				
				<el-table-column prop="status" label="门店状态" min-width="100" v-perms="['store:status']">
				    <template #default="{ row }">
						<!-- 
							active-text="营业"
							inactive-text="停业"
						-->
				        <el-switch
				            v-model="row.status"
				            :active-value="1"
				            :inactive-value="0"							
				            @change="changeSwitchStatus($event, row)"
				        />
				    </template>
				</el-table-column>
				
                <el-table-column label="创建时间" prop="createTime" min-width="120" />                
                <el-table-column label="操作" width="200" fixed="right">
                    <template #default="{ row }">
                        <el-button
                            v-perms="['store:edit']"
                            type="primary"
                            link
                            @click="handleEdit(row)"
                        >
                            编辑
                        </el-button>
                        <el-button
                            v-perms="['store:del']"
                            type="danger"
                            link
                            @click="handleDelete(row.storeId)"
                        >
                            删除
                        </el-button>
						<el-button
						    v-perms="['store:user']"
						    type="warning"
						    link
						    @click="handleAccount(row)"
						>
						    账号
						</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="flex justify-end mt-4">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <edit-popup
            v-if="showEdit"
            ref="editRef"
            @success="getLists"
            @close="showEdit = false"
        />
    </div>
</template>
<script lang="ts" setup name="store">
import { storeDelete, storeLists, apiStoreStatus } from '@/api/store/store'
import { usePaging } from '@/hooks/usePaging'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'

const editRef = shallowRef<InstanceType<typeof EditPopup>>()
const showEdit = ref(false)

const queryParams = reactive({   
    storeName: '',    
    priceType: '',  
    status: '',    
})

const { pager, getLists, resetPage, resetParams } = usePaging({
    fetchFun: storeLists,
    params: queryParams
})

const handleAdd = async () => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('add')
}

const handleEdit = async (data: any) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.getDetail(data)
}

const handleAccount = async (data: any) => {	
    showEdit.value = true
    await nextTick()
    editRef.value?.open('account')
    editRef.value?.getDetail(data)	
}

const handleDelete = async (storeId: number) => {
    await feedback.confirm('确定要删除？')
    await storeDelete({ storeId })
    feedback.msgSuccess('删除成功')
    getLists()
}

// 更改状态
const changeSwitchStatus = async (value: 0 | 1, data: any) => {
    apiStoreStatus({
            storeId: data.storeId,
            status: value
        }).catch(err => {
            getLists()
        })
}

getLists()
</script>
<style lang="scss" scoped>
	.dashed-border {
	  border-style: dashed;
	  border-width: 1px; /* 定义边框粗细 */
	  border-color: #bdbdbd; /* 定义边框颜色 */
	}
</style>
